import * as actionTypes from './constants';
import { fromJS } from 'immutable';
import { getHotSingerListRequest, getSingerListRequest } from '../../../api/request';
import { categoryTypes } from '../../../api/category'; 


// 需要修改的state有： singerList, enterLoading, pullDownLoading, pullUpLoading, pageCount
export const changeSingerList = (data) => ({
    type: actionTypes.CHANGE_SINGER_LIST,
    data: fromJS(data),
});

export const changePageCount = (data) => ({
    type: actionTypes.CHANGE_PAGE_COUNT,
    data,
});

export const changeEnterLoading = (data) => ({
    type: actionTypes.CHANGE_ENTER_LOADING,
    data: fromJS(data),
});

export const changePullUpLoading = (data) => ({
    type: actionTypes.CHANGE_PULLUP_LOADING,
    data,
});

export const changePullDownLoading = (data) => ({
    type: actionTypes.CHANGE_PULLDOWN_LOADING,
    data,
});


// 第一次加载热门歌手
export const getHotSingerList = () => {
    return (dispatch) => {
        getHotSingerListRequest(0).then(res => {
            const data = res.artists;
            dispatch(changeSingerList(data));
            dispatch(changeEnterLoading(false));
            dispatch(changePullDownLoading(false));
        }).catch(err => {
            console.log('热门歌手获取失败', err);
        })
    };
};

// 加载更多热门歌手
export const refreshMoreHotSingerList = () => {
    return (dispatch, getState) => {
        const pageCount = getState().getIn(['singers', 'pageCount']);
        const singerList = getState().getIn(['singers', 'singerList']).toJS();
        getHotSingerListRequest(pageCount).then(res => {
            const data = [...singerList, ...res.artists];
            dispatch(changeSingerList(data));
            dispatch(changePullUpLoading(data));
        }).catch(err => {
            console.log('热门歌手获取失败', err);
        });
    };
};

// 第一次加载对应类别歌手
export const getSingerList = (category, alpha) => {
    let type, area;
    if (category) {
        const categoryObj = categoryTypes.find(item => item.key === category);
        type = categoryObj.type;
        area = categoryObj.area;
    } else {
        type = '-1';
        area = '-1';
    }
    
    return (dispatch) => {
        getSingerListRequest(type, area, alpha ,0).then(res => {
            const data = res.artists;
            dispatch(changeSingerList(data));
            dispatch(changeEnterLoading(false));
            dispatch(changePullDownLoading(false));
        }).catch(err => {
            console.log('歌手数据获取失败！', err);
        });
    };
};


// 加载更多歌手
export const refreshMoreSingerList = (category, alpha) => {
    let type, area;
    if (category) {
        const categoryObj = categoryTypes.find(item => item.key === category);
        type = categoryObj.type;
        area = categoryObj.area;
    } else {
        type = '-1';
        area = '-1';
    };

    return (dispatch, getState) => {
        const pageCount = getState().getIn(['singers', 'pageCount']);
        const singerList = getState().getIn(['singers', 'singerList']).toJS();
        getSingerListRequest(type, area, alpha ,pageCount).then(res => {
            const data = [...singerList, ...res.artists];
            dispatch(changeSingerList(data));
            dispatch(changePullUpLoading(false));
        }).catch(err => {
            console.log('歌手数据获取失败！', err);
        });
    };
};





